<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2017/2/13
 * Time: 14:52
 */
namespace Application\Controller;

use Application\Service\SiteService;
use Application\Utils\Page;
use Infobird\Controller\BaseController;
use Infobird\Utils\LogUtils;

class SiteController extends BaseController
{
    public function indexAction(){
        $site = new SiteService();
        try{
            $page = 1; //默认页数
            $pageSize = 10; //每页显示条数
            if ($this->getParamValue("page")){
                $page = $this->getParamValue("page"); //当前页数
            }else {
                $page = 1;
            }
            $total = $site->contSite(); //总条数
            $total = isset($total[0]['COUNT(0)'])?$total[0]['COUNT(0)']:"0";
            $pageNum = ceil($total / $pageSize); //显示页数
            $result = $site->fetchAll($page,$pageSize);
            $pager = new Page();
            $url = $pager->createPage($page,$pageNum); //创建分页
            return array("result"=>$result,"page"=>$url);
        }catch (\Exception $e){
            LogUtils::log("t_site.log", 'index'.$e->getMessage(),array("result"=>"500"));
            exit;
        }
    }

    public function addSiteAction(){
        $city = isset($_REQUEST['siteCity'])?$_REQUEST['siteCity']:"";
        $adress = isset($_REQUEST['siteName'])?$_REQUEST['siteName']:"";
        $insert_dt = date("Y-m-d h:i:s");
        $arr = array(
            "city"=>$city,
            "adress"=>$adress,
            "insert_dt"=>$insert_dt
        );

        $site = new SiteService();
        try{
            $result = $site->addSite($arr);
            echo json_encode(array("result"=>"0","msg"=>"save ok"));
            exit;
        }catch (\Exception $e){
            LogUtils::log("t_site.log", 'addSite'.$e->getMessage(),array("result"=>"500"));
            exit;
        }
    }

    public function deleteAction(){
        $id = isset($_REQUEST['id'])?$_REQUEST['id']:"";
        $site = new SiteService();
        try{
            $result = $site->delete($id);
            echo json_encode(array("result"=>"0","msg"=>"del ok"));
            exit;
        }catch (\Exception $e){
            LogUtils::log("t_site.log", 'del'.$e->getMessage(),array("result"=>"500"));
            exit;
        }
    }
}